﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SysContext;


namespace DataAccessLayer
{
    public class TransactionReminderTable
    {
        SysDataContext Data = new SysDataContext();
        CategoriesTable Categorie = new CategoriesTable();
        TimeRuleTable TimeTable = new TimeRuleTable();
        AccountsTable AccountTable = new AccountsTable();

        /// <summary>
        /// Thêm dữ liệu lịch nhắc nhở vào bảng TransactionReminder.
        /// </summary>
        /// <param name="AccountID"></param>
        /// <param name="Payment"></param>
        /// <param name="Deposit"></param>
        /// <param name="CategoryID"></param>
        /// <param name="Description"></param>
        /// <param name="AutoCommit"></param>
        /// <param name="TimeRuleid"></param>
        /// <param name="Time"></param>
        public void InsertTransReminder(int AccountID, double Payment, double Deposit, int CategoryID, string Description, int AutoCommit, int TimeRuleid,string Time)
        {
            Data.InsertransactionReminder(AccountID, Payment, Deposit, CategoryID, Description, AutoCommit, TimeRuleid, Time);
        }        

        /// <summary>
        /// Lấy dữ liệu trong bảng TransactionReminder, bảng Category và bảng TimeRule
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="AccountType"></param>
        /// <returns>Description,Deposit,Payment,StartDate,EndDate,RepeatMode,Name,AutoCommit</returns>
        public Array TransReminder(int UserID, string AccountType)
        {
            // Kiểm tra loại tài khoản (TK thu chi, TK tiết kiệm).
            if (AccountType == "" || AccountType == "ALL")
            {

                var p = (from i in Data.TransactionReminders
                         join c in Data.Categories on i.CategoryId equals c.Id
                         join t in Data.TimeRules on i.TimeRuleId equals t.Id
                         select new
                         {
                             Mota = i.Description,
                             Tienthu = i.Deposit,
                             Tienchi = i.Payment,
                             BeginDate = t.StartDate,
                             EndDate = t.EndDate,
                             Time = t.RepeatMode,
                             TenChitieu = c.Name,
                             LapLai = i.AutoCommit
                         }).ToArray();
                return p.ToArray();
            }
            else
            {
                var p = (from i in Data.TransactionReminders
                         join c in Data.Categories on i.CategoryId equals c.Id
                         join t in Data.TimeRules on i.TimeRuleId equals t.Id
                         where i.AccountId == AccountTable.GetAccountId(UserID, AccountType)
                         select new
                         {
                             Mota = i.Description,
                             Tienthu = i.Deposit,
                             Tienchi = i.Payment,
                             BeginDate = t.StartDate,
                             EndDate = t.EndDate,
                             Time = t.RepeatMode,
                             TenChitieu = c.Name,
                             LapLai = i.AutoCommit
                         }).ToArray();
                return p.ToArray();
            }
        }

        /// <summary>
        /// Lấy toàn bộ dữ liệu từ bảng TransactionsReminder với điều kiện 
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="AccountType"></param>
        /// <param name="Date"></param>
        /// <returns></returns>
        public List<TransactionReminder> TransReminderAuto(int UserId, string AccountType,DateTime Date)
        {
            Data.GetChangeSet();
            var p = (from i in Data.TransactionReminders
                      join j in Data.Accounts on i.AccountId equals j.Id
                      join k in Data.TimeRules on i.TimeRuleId equals k.Id
                      where i.AccountId == AccountTable.GetAccountId(UserId, AccountType) && k.StartDate <= Date && k.EndDate >= Date
                     select i).ToList();                    
            return p.ToList();
        }

        /// <summary>
        /// Cập nhật thời điểm xảy ra giao dịch trong ngày tiếp theo
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Time"></param>
        public void UpdateTransReminderAuto(double Id,string Time)
        {
            TransactionReminder Reminder = Data.TransactionReminders.Single(p => p.Id == Id);
            Reminder.LastOccurance = DateTime.Parse(Time);         
            Data.SubmitChanges();
        }

        /// <summary>
        /// Đêm tổng số dòng trong bảng TransactionReminder.
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="AccountType"></param>
        /// <returns>SumRow</returns>
        public int TransReminderGetCount(int UserId, string AccountType)
        {
           return (from i in Data.TransactionReminders
                     join j in Data.Accounts on i.AccountId equals j.Id
                     where i.AccountId == AccountTable.GetAccountId(UserId, AccountType)
                     select i).Count();            
        }

        /// <summary>
        /// Lấy thời gian giao dịch trong TransactionReminders.
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="AccountType"></param>
        /// <param name="ID"></param>
        /// <returns>LastOccurance</returns>
        public DateTime? GetLastOccurance(int UserId, string AccountType ,double ID)
        {
            TransactionReminder Trans = (from i in Data.TransactionReminders
                                         join j in Data.Accounts on i.AccountId equals j.Id
                                         where i.AccountId == AccountTable.GetAccountId(UserId, AccountType) && i.Id == ID
                                         select i).Single();
            return Trans.LastOccurance;
        }

        /// <summary>
        /// Lấy danh sách 5lịch nhắc nhở
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns>StartDate,Description,Balance</returns>
        public Array GetReminder(int UserId)
        {
            var GetReminder = (from T in Data.TransactionReminders
                               join A in Data.Accounts on T.AccountId equals A.Id
                               join Ti in Data.TimeRules on T.TimeRuleId equals Ti.Id
                               where (Ti.StartDate > DateTime.Now) && A.UserId == UserId
                               select new
                               {
                                   Ti.StartDate,
                                   T.Description,
                                   Balan = T.Payment+T.Deposit
                               }).ToArray();
            return GetReminder.ToArray();
        }
        

    }
}
